package com.justech.mes.utils.excel;

import com.justech.mes.utils.plugins.transfer.Processer;
import com.justech.mes.utils.plugins.transfer.TransferFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <strong>含有字段转换器的导出工具</strong>
 * <warning style="color:red;">
 * In the new version of Excel tools, @ApiModelProperty is abandoned and replaced by @Title.
 * </warning>
 *
 * @author feifei.li
 */
public class ExcelForConverBeanUtil {
    public static void export(String fileName, List datas, Class<?> beanClass) {
        List<ExcelUtil.Entry> columns = ExportForBean.getHeadersForBean(beanClass);
        /*需要被导出的数据集合*/
        List<Object[]> objects = convert(datas, columns);
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        ExcelUtil.excelExport(fileName, ExportForBean.headers(columns), objects, servletRequestAttributes.getResponse());
    }

    private static List<Object[]> convert(List datas, List<ExcelUtil.Entry> columns) {
        Processer processor = TransferFactory.processor();
        /*需要被导出的数据集合*/
        List<Object[]> objects = new ArrayList<>(datas.size());
        /*通过反射获取属性的值*/
        for (Object o : datas) {
            Object[] rows = new Object[columns.size()];
            Class var1 = o.getClass();
            for (int i = 0; i < columns.size(); i++) {
                try {
                    Field var2 = null;
                    try {
                        var2 = var1.getDeclaredField(columns.get(i).getKey().toString());
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                    var2.setAccessible(true);
                    Object fieldValue = var2.get(o);
                    /*格式转换*/
                    fieldValue = processor.processSingleType(fieldValue, var2.getAnnotations());
                    rows[i] = fieldValue;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            objects.add(rows);
        }
        return objects;
    }

    private static List<Object[]> processDatas(List datas) {
        Processer processor = TransferFactory.processor();
        List<Object[]> arrs = new ArrayList<>();
        for (Object o : datas) {
            List<Object> objects = processor.processToList(o);
            List<Object> processToList = processor.processToList(objects);
            Object[] array = toArray(processToList);
            arrs.add(array);
        }
        return arrs;
    }

    private static Object[] toArray(List datas) {
        Processer processor = TransferFactory.processor();
        Object[] rs = new Object[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            Object o = datas.get(i);
            List<Object> objects = processor.processToList(o);
            List<Object> processToList = processor.processToList(objects);
            rs[i] = processToList;
        }
        return rs;
    }
}
